<html>
<head>
<title>CpcPaChi - Version 0.01</title>
<style type="text/css">
  span
  {
    font-family: Monospace, monospace, 'Courier New';
    font-size: 10pt;
    color: #000000;
  }

  .style_1
  {
    color: #d00000;
    background-color: #ffffff;
  }
  .style_4
  {
    color: #007f00;
    background-color: #ffffff;
  }
  .style_5
  {
    color: #00007f;
    background-color: #ffffff;
    font-weight: bold;
  }
  .style_6
  {
    color: #ff901e;
    background-color: #ffffff;
  }
  .style_10
  {
    color: #301010;
    background-color: #ffffff;
  }
  .style_11
  {
    color: #000000;
    background-color: #ffffff;
  }

</style>
</head>
<body bgcolor="#FFFFFF">
<h2>Un compilador sencillo paso a paso (1)</h2>
<p>Este primer acercamiento reconocer&aacute; s&oacute;lo la orden CLS, encargada de borrar la pantalla. La orden en lenguaje ensamblador de un Amstrad CPC equivalente es CALL &amp;BB6C, que en c&oacute;digo m&aacute;quina ser&iacute;a la secuencia de bytes CD, 6C, BB (expresados en hexadecimal). </p>
<p>  Para simplificar la prueba de los programas destino creados, el compilador no generar&aacute; ensamblador. Ni siquiera crear&aacute; un fichero ".BIN" de c&oacute;digo m&aacute;quina, sino un fuente en Basic capaz de "generar ese c&oacute;digo m&aacute;quina". As&iacute;, para una &uacute;nica orden CLS, se obtendr&iacute;a el siguiente programa, que lee los bytes en hexadecimal, los coloca a partir de la direcci&oacute;n 40.000 de la memoria y finalmente llama a esa direcci&oacute;n para poner el programa en marcha: 
</p>
<pre> 10 DATA CD,6C,BB: ' CALL &amp;BB6C 
 20 DATA C9: ' RET 
 30 longitud = 3 
 40 MEMORY 39999 
 50 FOR n=40000 TO 40000+longitud 
 60 READ a$:POKE n,VAL("&amp;"+a$) 
 70 NEXT 
 80 CALL 40000 
 
 </pre>

 <p>Con todas estas consideraciones, el primer acercamiento al compilador es muy sencillo:</p>

<ul>
  <li>Una funci&oacute;n que obtiene una orden (s&oacute;lo una) del usuario, desde el teclado. </li>
  <li>Una funci&oacute;n que analiza la orden, y da un mensaje de error si no es CLS. </li>
  <li>Una funci&oacute;n que genera el c&oacute;digo de destino (que por ahora es fijo). <br>
  </li>
</ul>
<br>
El fuente ser&iacute;a as&iacute;: <br>
<br>
<br>
<p>
<span class="style_1">/*&nbsp;cpcPaChi;</span><br />
<br />
&nbsp;&nbsp;&nbsp;<span class="style_1">Un&nbsp;compilador&nbsp;de&nbsp;Pascal&nbsp;chiquitito&nbsp;para&nbsp;CPC</span><br />
&nbsp;&nbsp;&nbsp;<span class="style_1">Por&nbsp;Nacho&nbsp;Cabanes&nbsp;-&nbsp;Version&nbsp;0.01cs,&nbsp;preliminar</span><br />
&nbsp;&nbsp;&nbsp;<span class="style_1">(en&nbsp;C#)</span><br />
<br />
&nbsp;&nbsp;&nbsp;<span class="style_1">Versiones&nbsp;hasta&nbsp;la&nbsp;fecha:</span><br />
<br />
&nbsp;&nbsp;&nbsp;<span class="style_1">Num.&nbsp;&nbsp;&nbsp;Fecha&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Cambios</span><br />
&nbsp;&nbsp;&nbsp;<span class="style_1">---------------------------------------------------</span><br />
&nbsp;&nbsp;&nbsp;<span class="style_1">0.01cs&nbsp;&nbsp;15-Oct-2010&nbsp;&nbsp;Preliminar:&nbsp;solo&nbsp;acepta&nbsp;CLS&nbsp;por&nbsp;teclado</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="style_1">y&nbsp;genera&nbsp;el&nbsp;codigo&nbsp;correspondiente</span><br />
<span class="style_1">*/</span><br />
<br />
<span class="style_5">using&nbsp;</span><span class="style_11">System</span><span class="style_10">;</span><br />
<span class="style_5">using&nbsp;</span><span class="style_11">System</span><span class="style_10">.</span><span class="style_11">IO</span><span class="style_10">;</span><br />
<br />
<span class="style_5">class&nbsp;</span><span class="style_19">cpcpachi</span><br />
<span class="style_10">{</span><br />
&nbsp;&nbsp;<span class="style_5">static&nbsp;string&nbsp;</span><span class="style_11">orden</span><span class="style_10">;</span><br />
&nbsp;&nbsp;<span class="style_5">static&nbsp;</span><span class="style_11">StreamWriter&nbsp;ficheroDestino</span><span class="style_10">;</span><br />
<br />
<br />
&nbsp;&nbsp;<span class="style_1">/*&nbsp;Obtiene&nbsp;una&nbsp;orden&nbsp;del&nbsp;programa&nbsp;fuente.&nbsp;En&nbsp;esta&nbsp;version,&nbsp;solo</span><br />
&nbsp;&nbsp;<span class="style_1">es&nbsp;una&nbsp;unica&nbsp;orden,&nbsp;y&nbsp;solo&nbsp;se&nbsp;admite&nbsp;"CLS"&nbsp;*/</span><br />
&nbsp;&nbsp;<span class="style_5">static&nbsp;void&nbsp;</span><span class="style_11">obtenerOrden</span><span class="style_10">()</span><br />
&nbsp;&nbsp;<span class="style_10">{</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;<span class="style_11">Console</span><span class="style_10">.</span><span class="style_11">Write</span><span class="style_10">(</span><span class="style_6">"Introduzca&nbsp;la&nbsp;orden&nbsp;a&nbsp;traducir:&nbsp;"</span><span class="style_10">);</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;<span class="style_11">orden&nbsp;</span><span class="style_10">=&nbsp;</span><span class="style_11">Console</span><span class="style_10">.</span><span class="style_11">ReadLine</span><span class="style_10">();</span><br />
&nbsp;&nbsp;<span class="style_10">}</span><br />
<br />
&nbsp;&nbsp;<span class="style_1">/*&nbsp;Analiza&nbsp;la&nbsp;orden&nbsp;que&nbsp;el&nbsp;usuario&nbsp;ha&nbsp;dado,&nbsp;y&nbsp;sale&nbsp;con&nbsp;un&nbsp;mensaje</span><br />
&nbsp;&nbsp;<span class="style_1">de&nbsp;error&nbsp;si&nbsp;es&nbsp;incorrecta&nbsp;*/</span><br />
&nbsp;&nbsp;<span class="style_5">static&nbsp;void&nbsp;</span><span class="style_11">analizarOrden</span><span class="style_10">()</span><br />
&nbsp;&nbsp;<span class="style_10">{</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;<span class="style_5">if&nbsp;</span><span class="style_10">(&nbsp;</span><span class="style_11">orden</span><span class="style_10">.</span><span class="style_11">ToUpper</span><span class="style_10">()&nbsp;!=&nbsp;</span><span class="style_6">"CLS"&nbsp;</span><span class="style_10">)</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;<span class="style_10">{</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="style_11">Console</span><span class="style_10">.</span><span class="style_11">WriteLine</span><span class="style_10">(</span><span class="style_6">"Error:&nbsp;orden&nbsp;no&nbsp;reconocida.&nbsp;Se&nbsp;esperaba:&nbsp;CLS"</span><span class="style_10">);</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="style_11">Environment</span><span class="style_10">.</span><span class="style_11">Exit</span><span class="style_10">(</span><span class="style_4">1</span><span class="style_10">);</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;<span class="style_10">}</span><br />
&nbsp;&nbsp;<span class="style_10">}</span><br />
<br />
&nbsp;&nbsp;<span class="style_1">/*&nbsp;Genera&nbsp;el&nbsp;codigo&nbsp;de&nbsp;destino:&nbsp;codigo&nbsp;maquina&nbsp;de&nbsp;Amstrad&nbsp;CPC</span><br />
&nbsp;&nbsp;<span class="style_1">dentro&nbsp;de&nbsp;un&nbsp;cargador&nbsp;en&nbsp;Basic&nbsp;*/</span><br />
&nbsp;&nbsp;<span class="style_5">static&nbsp;void&nbsp;</span><span class="style_11">generarCodigo</span><span class="style_10">()</span><br />
&nbsp;&nbsp;<span class="style_10">{</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;<span class="style_11">ficheroDestino&nbsp;</span><span class="style_10">=&nbsp;</span><span class="style_11">File</span><span class="style_10">.</span><span class="style_11">CreateText</span><span class="style_10">(</span><span class="style_6">"salida.bas"</span><span class="style_10">);</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;<span class="style_11">ficheroDestino</span><span class="style_10">.</span><span class="style_11">WriteLine</span><span class="style_10">(</span><span class="style_6">"10&nbsp;DATA&nbsp;CD,6C,BB:&nbsp;'&nbsp;CALL&nbsp;&amp;BB6C"&nbsp;</span><span class="style_10">);</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;<span class="style_11">ficheroDestino</span><span class="style_10">.</span><span class="style_11">WriteLine</span><span class="style_10">(</span><span class="style_6">"20&nbsp;DATA&nbsp;C9:&nbsp;'&nbsp;RET"&nbsp;</span><span class="style_10">);</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;<span class="style_11">ficheroDestino</span><span class="style_10">.</span><span class="style_11">WriteLine</span><span class="style_10">(</span><span class="style_6">"30&nbsp;longitud&nbsp;=&nbsp;3"&nbsp;</span><span class="style_10">);</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;<span class="style_11">ficheroDestino</span><span class="style_10">.</span><span class="style_11">WriteLine</span><span class="style_10">(</span><span class="style_6">"40&nbsp;MEMORY&nbsp;39999"&nbsp;</span><span class="style_10">);</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;<span class="style_11">ficheroDestino</span><span class="style_10">.</span><span class="style_11">WriteLine</span><span class="style_10">(</span><span class="style_6">"50&nbsp;FOR&nbsp;n=40000&nbsp;TO&nbsp;40000+longitud"&nbsp;</span><span class="style_10">);</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;<span class="style_11">ficheroDestino</span><span class="style_10">.</span><span class="style_11">WriteLine</span><span class="style_10">(</span><span class="style_6">"60&nbsp;READ&nbsp;a$:POKE&nbsp;n,VAL(\"&amp;\"+a$)"&nbsp;</span><span class="style_10">);</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;<span class="style_11">ficheroDestino</span><span class="style_10">.</span><span class="style_11">WriteLine</span><span class="style_10">(</span><span class="style_6">"70&nbsp;NEXT"&nbsp;</span><span class="style_10">);</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;<span class="style_11">ficheroDestino</span><span class="style_10">.</span><span class="style_11">WriteLine</span><span class="style_10">(</span><span class="style_6">"80&nbsp;CALL&nbsp;40000"&nbsp;</span><span class="style_10">);</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;<span class="style_11">ficheroDestino</span><span class="style_10">.</span><span class="style_11">Close</span><span class="style_10">();</span><br />
&nbsp;&nbsp;<span class="style_10">}</span><br />
<br />
&nbsp;&nbsp;<span class="style_1">/*&nbsp;Cuerpo&nbsp;del&nbsp;programa&nbsp;*/</span><br />
&nbsp;&nbsp;<span class="style_5">static&nbsp;void&nbsp;</span><span class="style_11">Main</span><span class="style_10">()</span><br />
&nbsp;&nbsp;<span class="style_10">{</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;<span class="style_11">obtenerOrden</span><span class="style_10">();</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;<span class="style_11">analizarOrden</span><span class="style_10">();</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;<span class="style_11">generarCodigo</span><span class="style_10">();</span><br />
&nbsp;&nbsp;<span class="style_10">}</span><br />
<span class="style_10">}</span><br />

</body>
</html>

